<?php
/**
 * @license http://www.gnu.org/licenses/lgpl.html LGPL
 * @author Harlan Iverson <h dot iverson at gmail dot com>
 * @copyright Copyright (c) 2006, Harlan Iverson.
 * @version $Id$
 */

require_once( "PhpFaces/Context/FacesContext.php" );
require_once( "PhpFaces/Component/UIComponent.php" );

class PhpFaces_Render_RenderKitFactory {
	const HTML_BASIC_RENDER_KIT = "HTML_BASIC";
	const LOGGER = "PhpFaces.Component.UI.UIComponentBase";
	
	private $renderKits;
	
	private static $instance;
	public static function getInstance() {

		if( self::$instance == null ) {
			self::$instance = new PhpFaces_Render_RenderKitFactory();
		}
		return self::$instance;
	}
	
	private function __construct() {
		$this->renderKits = array();
	}
	
	public function getRenderKit( PhpFaces_Context_FacesContext $facesContext, $renderKitId = self::HTML_BASIC_RENDER_KIT ) {
		if( !array_key_exists( $renderKitId, $this->renderKits ) ) {
			// TODO throw some kind of exception
			$logger = LoggerFactory::GetLogger( self::LOGGER );
			$logger->warn( "There was an attempt to use the render kit '$renderKitId' which does not exist." );
			
			
			
			throw new Exception( "RenderKit '$renderKitId' doesn't exist.");
		}
	
		return $this->renderKits[ $renderKitId ];
	}
	
	public function addRenderKit( $renderKitId, PhpFaces_Render_RenderKit $renderKit ) {
		$this->renderKits[ $renderKitId ] = $renderKit;
	}
	
	public function getRenderKitIds() {
		return new ArrayObject( array_keys($this->renderKits) );
	}
}

abstract class PhpFaces_Render_RenderKit {
	public abstract function addRenderer( $family, $rendererType, PhpFaces_Render_Renderer $renderer );
	public abstract function getRenderer( $family, $rendererType );
}

abstract class PhpFaces_Render_Renderer {
	public abstract function encodeBegin( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIComponent $component );
	public abstract function encodeEnd( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIComponent $component );
	public abstract function encodeChildren( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIComponent $component );
	public abstract function getRendersChildren();
}


class PhpFaces_Render_RenderUtils {
	public static function getStringValue( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIComponent $component ) {
		if( !($component instanceof PhpFaces_ValueHolder) ) {
			// throw new IllegalArgumentException("Component : "+getPathToComponent(component)+"is not a ValueHolder");	
		}
		
		if( $component instanceof PhpFaces_EditableValueHolder ) {
			$submittedValue = $component->getSubmittedValue();
			return 	$submittedValue;
		}
	}	
}


?>